<?php
/**
 * @package Joomla
 * @subpackage Jcampaignmonitor
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jcampaignmonitor is a derivative work of the excellent Campaign Monitor Module (from Van Eldijk Studios) and CampaignMonitor Ajax subscription Module (from Joomailer)
 * see http://www.vaneldijk.nl/ and http://www.joomailer.com/ for more information
 *
 * Jcampaignmonitor uses :
 * CampaignMonitorLib (CMBase.php => http://code.google.com/p/campaignmonitor-php/)
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * 1 000 free "Farm Fresh Web Icons" => http://www.fatcow.com/free-icons/
 * extendedlistnamevalue.php & extendedlist.js are inspired by RocketTheme !
 *
 * Jcampaignmonitor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define ('ABSOLUTE_PATH', dirname(__FILE__) );
define ('RELATIVE_PATH', 'modules' . DS . 'mod_jcampaignmonitor');
define ('JPATH_BASE', str_replace(RELATIVE_PATH, "", ABSOLUTE_PATH));

require_once( 'helper.php' );

require_once ( JPATH_BASE . DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE . DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$language = JFactory::getLanguage();
$language->load('mod_jcampaignmonitor');

JRequest::checkToken( 'get' ) or die( 'Invalid Token' );

//get id module
$id  = JRequest::getVar( 'id', '', 'get', 'int' );
// get parameters with id module
$parameters = modJCampaignMonitorHelper::getParams($id);
$params = new JParameter( $parameters );
//get name and email variables
$name  = JRequest::getVar( 'jcm-name', '', 'get', 'string' );
$email = JRequest::getVar( 'jcm-mail', '', 'get', 'string' );
$list = JRequest::getVar( 'jcm-list', '', 'get', 'int' );

if( !empty( $email ) && !empty($list) ) {
	global $mainframe;
	require_once( dirname( __FILE__ ) . DS . 'assets'. DS .'api' . DS . 'CMBase.php' );
	
	$listid = $params->get( 'jcampaignmonitor_extendedlist_value_'.$list );
	$cm = new CampaignMonitor( $params->get( 'apikey' ), null, null, $listid, 'soap' );
	$cm->subscriberAdd( $email, $name );
	$responsecp = $cm->xml2array( $cm->debug_response, '/soap:Envelope/soap:Body/Subscriber.AddResponse/Subscriber.AddResult' );

	
	if( in_array( $responsecp['Code'], array( '0', '1', '100', '101', '204' ) ) ) {
		//see return code => http://www.campaignmonitor.com/api/method/subscriber-add/
		switch( $responsecp['Code'] ) {
			case '0':
			$response['msg'] = JText::_( 'SUBSCRIPTION SUCCESS' );
            $response['success'] = 'true';
			break;

            case '1':
			$response['msg'] = JText::_( 'INVALID EMAIL ADDRESS' );
            $response['success'] = 'false';
			break;

			case '100':
			$response['msg'] = JText::_( 'INVALID API KEY' );
            $response['success'] = 'false';
			break;

			case '101':
			$response['msg'] =  JText::_( 'INVALID LISTID' );
            $response['success'] = 'false';
			break;

			case '204':
			$response['msg'] = JText::_( 'IN SUPPRESSIONLIST' );
            $response['success'] = 'false';
			break;
			
			case '205':
			$response['msg'] = JText::_( 'IS DELETED' );
            $response['success'] = 'false';
			break;
			
			case '206':
			$response['msg'] = JText::_( 'IS UNSUBSCRIBED' );
            $response['success'] = 'false';
			break;
			
			case '207':
			$response['msg'] = JText::_( 'IS BOUNCED' );
            $response['success'] = 'false';
			break;
			
			case '208':
			$response['msg'] = JText::_( 'IS UNCONFIRMED' );
            $response['success'] = 'false';
			break;
			}

		}
		
		else 
		{
		$response['msg'] = JText::_( 'ERROR NAME EMAIL' ).' '.$responsecp['Code'];
		$response['success'] = 'false';
		}
		
	}
	else if ( empty($list) )
	{
	$response['msg'] = JText::_( 'ERROR SELECT LIST' );
	$response['success'] = 'false';
	}

// Get the document object.
$document =& JFactory::getDocument(); 
// Set the MIME type for JSON output.
$document->setMimeEncoding( 'application/json' );

if( function_exists('json_encode') )
	{
	   echo ( json_encode( $response ) );
	}
	else
	{
	   //--seems we are in PHP < 5.2... or json_encode() is disabled
	   require_once( dirname( __FILE__ ) . DS . 'assets'. DS .'json' . DS . 'json.php' );
	   $json = new Services_JSON();
	   echo $json->encode( $response );
	}
?>